package com.example.todofinally;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TimePicker;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;

import com.example.todofinally.sql.SpHelper;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;
@SuppressWarnings({"all"})
public class edit_Activity extends AppCompatActivity {
    private EditText start_time,end_time,edit_title,date,edit_content;
    private ImageView image;
    private SpHelper mHelper;
    private  ImageButton  dateChoose_button;
    private  ImageButton  starttimeChoose_button;
    private  ImageButton  endtimeChoose_button;
    final int TAKE_PHOTO=1;
    ImageView iv_photo;
    Uri imageUri;
    private String id;
    private Bitmap bitmap;





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);
        mHelper = new SpHelper(this);
        start_time = findViewById(R.id.start_time);
      /*  end_time = findViewById(R.id.end_time);*/
        image=findViewById(R.id.image);
        date=findViewById(R.id.add_date);
        edit_content =findViewById(R.id.edit_content);
        edit_title = findViewById(R.id.edit_title);
        dateChoose_button=findViewById(R.id.date_button);
        Intent intent=getIntent();
        id=intent.getStringExtra("id");
        if(id!=null)
        {
          edit_content.setText(mHelper.query3(id).editContent);
          date.setText(mHelper.query3(id).date);
          start_time.setText(mHelper.query3(id).startTime);
          edit_title.setText(mHelper.query3(id).editTitle);
            InputStream inputStream = null;
            try {
//                Log.d("123123123123123231", "onCreate: "+mHelper.query3(id).photo);
                imageUri = Uri.parse(mHelper.query3(id).photo);
                inputStream = getContentResolver().openInputStream(imageUri);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            image.setImageBitmap(bitmap);
           /* ByteArrayOutputStream os = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG,100,os);
            try {
                InputStream inputStream=getContentResolver().openInputStream(Uri.parse(mHelper.query3(id).photo));
                bitmap= BitmapFactory.decodeStream(inputStream);
                iv_photo.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }*/
        }
        dateChoose_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDate(edit_Activity.this,date);
            }
        });

        starttimeChoose_button=findViewById(R.id.starttime_button);
        starttimeChoose_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               getTime(edit_Activity.this,start_time);
            }
        });


/*        endtimeChoose_button=findViewById(R.id.endtime_button);
        endtimeChoose_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getTime(edit_Activity.this,end_time);

            }
        });*/


        ImageButton btn_1 = findViewById(R.id.btn_takephoto);
        iv_photo = findViewById(R.id.image);
        btn_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name= String.valueOf(new Date().getTime());
                File output = new File(getExternalCacheDir(), name);
                try {
                    if (output.exists()) {
                        output.delete();
                    }
                    output.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (Build.VERSION.SDK_INT >= 24) {
//图片的保存路径
                    imageUri = FileProvider.getUriForFile(edit_Activity.this, "com.example.takephoto.fileprovider", output);
                } else {
                    imageUri = Uri.fromFile(output);
                }
                //跳转界面到系统自带的拍照界面
                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(intent, TAKE_PHOTO);
            }
        });

    }
    public void intentMainActivity(View view) {
        finish();
    }

    public void addClick(View view) {
        String add_date = date.getText().toString();
        String starttime=start_time.getText().toString();
/*
        String endtime=end_time.getText().toString();
*/
        String edittitle=edit_title.getText().toString();
        String editcontent=edit_content.getText().toString();
        ContentValues contentValues = new ContentValues();
        contentValues.put("startTime",starttime);
/*
        contentValues.put("endTime",endtime);
*/
        contentValues.put("editTitle",edittitle);
        contentValues.put("content",editcontent);
        contentValues.put("date",add_date);
        contentValues.put("status",0);//0代表未完成
       /* ByteArrayOutputStream os = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG,100,os);*/
        contentValues.put("photo", String.valueOf(imageUri));
        if(id!=null)
        {
            mHelper.updateData(contentValues,id);
        }
         else
             mHelper.inserData(contentValues);
        Toast.makeText(this, "增加/修改成功！", Toast.LENGTH_SHORT).show();
        finish();
    }
    public void  getDate(Context context, EditText editText){
        Calendar calendar=Calendar.getInstance();
        new DatePickerDialog(context,
                new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                        month++;
                        if(month<10&&day<10){
                            editText.setText(year+"年"+"0"+month+"月"+"0"+day+"日");
                        } else if(month<10&&day>=10){
                            editText.setText(year+"年"+"0"+month+"月"+day+"日");
                        }else if (month>=10&&day<10){
                            editText.setText(year+"年"+month+"月"+"0"+day+"日");
                        }else {
                            editText.setText(year+"年"+month+"月"+day+"日");
                        }
                    }
                },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
    }

    public void  getTime(Context context, EditText editText){
        Calendar calendar=Calendar.getInstance();
        int hour=calendar.get(calendar.HOUR);
        int min=calendar.get(calendar.MINUTE);
        new TimePickerDialog(context,
                new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker timePicker, int hour, int min) {
            editText.setText(hour+""+":"+min+"");
                if(hour<10&&min<10){
                    editText.setText("0"+hour+""+":"+"0"+min+"");
                } else if(hour<10&&min>=10){
                    editText.setText("0"+hour+""+":"+min+"");
                }else if (hour>=10&&min<10){
                    editText.setText(hour+""+":"+"0"+min+"");
                }else {
                    editText.setText(hour+""+":"+min+"");
                }
            }
        },hour,min,true).show();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==TAKE_PHOTO)
        {
            try {
                Log.d("接收图片", "onActivityResult: "+imageUri);
                InputStream inputStream=getContentResolver().openInputStream(imageUri);
                bitmap= BitmapFactory.decodeStream(inputStream);
                iv_photo.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}